package st.business.tepth.business.maintainplan;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import io.reactivex.disposables.Disposable;
import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.MaintainPlanBean;
import st.business.tepth.config.Url;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-06-04 16:04
 */

public class MaintainPlanModel implements MaintainPlanContract.Model {

    @Override
    public Disposable getMaintainPlanList(String token, String date,
                                          final OnDataConvertListener<List<MaintainPlanBean>> listener) {
        return RxRestClient.builder()
                .url(Url.GetMaintainPlanList)
                .params("token", token)
                .params("date", date)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(convertToPlanList(response));
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    private List<MaintainPlanBean> convertToPlanList(String response) {
        return new ObjConverter<List<MaintainPlanBean>>().convert(response
                , new TypeReference<List<MaintainPlanBean>>() {
                });
    }
}
